package com.web.magic.catcher;

import java.util.EnumMap;
import java.util.Map;

import com.web.magic.catcher.model.SearchEnum;
import com.web.magic.catcher.model.Top;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

public class Processor {
	
	private static final Map<SearchEnum, String> map = new EnumMap<>(SearchEnum.class);
	
	static{
		map.put(SearchEnum.CURRENT, "http://top.baidu.com/");
		map.put(SearchEnum.SEVEN, "http://top.baidu.com/");
		map.put(SearchEnum.MOVIE, "http://top.baidu.com/category?c=1&fr=topindex");
	}
	
	private static final String AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36";
	private static final int RETRYTIMES = 3;
	private static final int SLEEP_TIME = 1000;
	private static final int SLEEP_OUT = 6000;
	private static final String HEADER = "Accept-Encoding";
	
	private static Site site = Site.me().setRetryTimes(RETRYTIMES).setSleepTime(SLEEP_TIME).setTimeOut(SLEEP_OUT) .addHeader(HEADER, "/").setUserAgent(AGENT);
	
	private static class Current implements PageProcessor{
		@Override
		public void process(Page page) {
			Selectable se = page.getHtml().xpath("//*[@id=\"hot-list\"]");
        	for(int i = 1; i < 11; i++){
        		Top cr = new Top();
        		cr.setName(se.xpath("//li[" + i + "]/a/text()").get());
        		cr.setNumber(i + "");
        		cr.setLevel(se.xpath("//li[" + i + "]").$("span[class^=icon-]", "text").get());
        		cr.setHref(se.xpath("//li[" + i + "]/a/@href").get());
        		page.putField(cr.getName(), cr);
        	}
		}
		@Override
		public Site getSite() {
			return Processor.site;
		}
	}
	private static class Seven implements PageProcessor{
		@Override
		public void process(Page page) {
			Selectable se = page.getHtml().xpath("//*[@id=\"main\"]/div[1]/div[1]/div[3]/div[2]/ul");
			for(int i = 1; i < 11; i++){
				Top cr = new Top();
				cr.setName(se.xpath("//li[" + i + "]/a[1]/text()").get());
				cr.setNumber(i + "");
				cr.setLevel(se.xpath("//li[" + i + "]").$("span[class^=icon-]", "text").get());
				cr.setHref(se.xpath("//li[" + i + "]/a[1]/@href").get());
				page.putField(cr.getName(), cr);
			}
		}
		@Override
		public Site getSite() {
			return Processor.site;
		}
	}
	private static class Movie implements PageProcessor{
		@Override
		public void process(Page page) {
			Selectable se = page.getHtml().xpath("//*[@id=\"main\"]/div[2]/div[2]/div/div/div/ul");
			for(int i = 1; i < 11; i++){
				Top cr = new Top();
				cr.setName(se.xpath("//li[" + i + "]/div[1]/a[1]/text()").get());
				cr.setNumber(i + "");
				cr.setLevel(se.xpath("//li[" + i + "]").$("span[class^=icon-]", "text").get());
				cr.setHref(se.xpath("//li[" + i + "]/div[1]/a[1]/@href").get());
				page.putField(cr.getName(), cr);
			}
		}
		@Override
		public Site getSite() {
			return Processor.site;
		}
	}
	
	public static String getUrl(SearchEnum key){
		return map.get(key);
	}
	public static PageProcessor get(SearchEnum key){
		switch(key){
		case CURRENT:
			return new Current();
		case SEVEN:
			return new Seven();
		case MOVIE:
			return new Movie();
		default:
			throw new RuntimeException("无对应的搜索项" + key);
		}
	}
}
